﻿Imports QL_GioiThieu_NhaThue.sqlDataProvider
Imports System.Data.SqlClient

Public Class LoaiTaiKhoanDAO
    Public Shared Function GetAll() As List(Of LoaiTaiKhoanDTO)
        Dim listLoaiTaiKhoan As New List(Of LoaiTaiKhoanDTO)
        Dim cn As SqlConnection = New SqlConnection(_strConnectionString)
        Try
            Dim cmd As New SqlCommand()
            cmd.CommandType = CommandType.StoredProcedure
            cmd.CommandText = "select_loaitaikhoan"
            cmd.Connection = cn
            cn.Open()

            Dim rd As SqlDataReader = cmd.ExecuteReader()
            While rd.Read()
                Dim loaitaikhoan As LoaiTaiKhoanDTO = New LoaiTaiKhoanDTO()
                loaitaikhoan.iMaLoaiTaiKhoan = rd.GetInt32(0)
                loaitaikhoan.strTenLoaiTaiKhoan = rd.GetString(1)

                listLoaiTaiKhoan.Add(loaitaikhoan)
            End While
            cn.Close()
        Catch ex As Exception
            MessageBox.Show("Phát hiện lỗi!" + Environment.NewLine + ex.Message, "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            If cn.State = ConnectionState.Open Then
                cn.Close()
            End If
        End Try
        
        Return listLoaiTaiKhoan
    End Function

    Public Shared Sub Delete(ByVal paraID As SqlParameter)
        Dim cn As SqlConnection = New SqlConnection(_strConnectionString)
        Dim cmd As New SqlCommand()
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = ""
        cmd.Connection = cn

        cmd.Parameters.Add(paraID)
        cn.Open()
        cmd.ExecuteNonQuery()
        cn.Close()
    End Sub

    Public Shared Sub Update(ByVal sqlParameters As SqlParameter())
        Dim cn As SqlConnection = New SqlConnection(_strConnectionString)
        Dim cmd As New SqlCommand()
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = ""
        cmd.Connection = cn

        cmd.Parameters.AddRange(sqlParameters)
        cn.Open()
        cmd.ExecuteNonQuery()
        cn.Close()
    End Sub

End Class
